﻿function crearBus() {
    var Placa = $("#tPlaca").val();
    if (Placa != "") {
        var Modelo = $("#tModelo").val();
        var Color = $("#tColor").val();
        var NumeroMotor = $("#tNumeroMotor").val();
        var Password1 = $("#tPassword1").val();
        var Password2 = $("#tPassword2").val();
        if (Password1 == Password2) {
            var parametro = {
                Placa: "'" + Placa + "'",
                Modelo: "'" + Modelo + "'",
                Color: "'" + Color + "'",
                NumeroMotor: "'" + NumeroMotor + "'",
                Password: "'" + Password1 + "'"
            };
            var DirSer = localStorage.getItem("DirSerUsu");
            var ruta = DirSer + "/Servicios/ServiciosJusTime.asmx/NuevoBus";
            $.ajax({
                type: "GET",
                url: ruta,
                data: parametro,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {
                    if (result.d == "ok") {
                        $("#tBuses").append("<tr id='" + Placa + "'  onclick='EliminarBus(id)' ><td><strong>" + Placa + "</strong></td><td><strong>" + Modelo + "</strong></td><td><strong>" + Color + "</strong></td><td><strong>" + NumeroMotor + "</strong></td><td><strong>" + Password1 + "</strong></td></tr>");
                        $("#tPlaca").val("");
                        $("#tModelo").val("");
                        $("#tColor").val("");
                        $("#tNumeroMotor").val("");
                        $("#tPassword1").val("");
                        $("#tPassword2").val("");
                    } else {
                        alert("Error: Verifique la informacion\nProbablemente esta placa ya existe...");
                    }
                },
                error: function (jqXHR, status, error) {
                    alert(error + "-" + jqXHR.responseText);
                }
            });
        } else {
            alert("Error: Password no conciden...");
        }
    } else {
        alert("Error: Olvido la placa del bus, es un campo obligatirio...");
    }
}

function infoBuses() {
    var parametro = {};
    document.getElementById("tBuses").innerHTML = "";
    $("#tBuses").append("<tr><td><strong>Placa</strong></td><td><strong>Modelo</strong></td><td><strong>Color</strong></td><td><strong>Numero de motor</strong></td><td><strong>Password</strong></td></tr>");
    var DirSer = localStorage.getItem("DirSerUsu");
    var ruta = DirSer + "/Servicios/ServiciosJusTime.asmx/ObtenerTodosBuses";
    $.ajax({
        type: "GET",
        url: ruta,
        data: parametro,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            var lbuses = (typeof result.d) == 'string' ? eval('(' + result.d + ')') : result.d;
            $.each(lbuses, function (index, item) {
                $("#tBuses").append("<tr id='" + item.Placa + "' onclick='EliminarBus(id)'><td><strong>" + item.Placa + "</strong></td><td><strong>" + item.Modelo + "</strong></td><td><strong>" + item.Color + "</strong></td><td><strong>" + item.NumeroMotor + "</strong></td><td><strong>" + item.Password + "</strong></td></tr>");
            });
        },
        error: function (jqXHR, status, error) {
            alert(error + "-" + jqXHR.responseText);
        }
    });
}

function EliminarBus(Placa1) {
    var confir = window.confirm("¿Desea eliminar el bus de placa: " + Placa1 + "?");
    if (confir) {
        var parametro = {
            Placa: "'" + Placa1 + "'"
        };
        var DirSer = localStorage.getItem("DirSerUsu");
        var ruta = DirSer + "/Servicios/ServiciosJusTime.asmx/EliminarBus";
        $.ajax({
            type: "GET",
            url: ruta,
            data: parametro,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                if (result.d == "true") {
                    alert("Se elimino el bus de placa: " + Placa1);
                    infoBuses();
                } else {
                    alert("Error en operacion...");
                }
            },
            error: function (jqXHR, status, error) {
                alert(error + "-" + jqXHR.responseText);
            }
        });
    }
}
